/*************************************************
* Title：TinyToolKit
* Author：Opportunity 
* Describe：GameObject类的拓展
* CreateTime：2020-04-05 19:39:40
* Version：1.0
* Modify Recorder：
*************************************************/
using System;
using UnityEngine;

namespace TinyToolKit
{
    public static class GameObjectExt
    {
        /// <summary>
        /// 获取组件，没有组件则增加
        /// </summary>
        /// <returns>返回该组件</returns>
        /// <param name="obj">需要获取组件的对象</param>
        /// <typeparam name="T">组件的类型</typeparam>
        public static T GetOrAddComponent<T>(this GameObject obj) where T : Component
        {
            T comp = obj.GetComponent<T>();
            if (comp == null)
                comp = obj.AddComponent<T>();

            return comp;  
        }

        /// <summary>
        /// 获取组件，没有组件则增加
        /// </summary>
        /// <returns>返回该组件</returns>
        /// <param name="obj">需要获取组件的对象</param>
        /// <param name="type">组件的类型</param>
        public static Component GetOrAddComponent(this GameObject obj, Type type)
        {
            Component comp = obj.GetComponent(type);
            if (comp == null)
                comp = obj.AddComponent(type);

            return comp;
        } 

        /// <summary>
        /// 销毁自身对象
        /// </summary>
        /// <param name="obj">需要销毁的对象</param>
        public static void DestroySelf(this GameObject obj)
        {
            GameObject.Destroy(obj); 
        }
        
        /// <summary>
        /// 等待一定时间销毁自身
        /// </summary>
        /// <param name="obj">需要销毁的对象</param>
        /// <param name="time">销毁等待的时间</param>
        public static void DestroySelf(this GameObject obj,float time)
        {
            GameObject.Destroy(obj,time); 
        }
        
        /// <summary>
        /// 立刻销毁自身
        /// </summary>
        /// <param name="obj">需要立刻销毁的对象</param>
        public static void DestroyImmediate(this GameObject obj)
        {
            GameObject.DestroyImmediate(obj);  
        }

        /// <summary>
        /// 在场景加载时，不销毁自身
        /// </summary>
        /// <param name="obj">不销毁的对象</param>
        public static void DontDestroyOnLoad(this GameObject obj)
        {
            GameObject.DontDestroyOnLoad(obj);
        }

        /// <summary>
        /// 查找子物体，如果不存在则创建
        /// </summary>
        public static GameObject FindOrCreate(this GameObject obj,string name)
        {
            Transform tran = obj.transform.Find(name);
            if (tran == null)
            {
                tran = new GameObject(name).transform;
                obj.transform.SetStandardChild(tran);
            }
            
            return tran.gameObject;
        }

        /// <summary>
        /// 判断当前对象是否为空
        /// </summary>
        /// <param name="obj">需要判断是否为空的对象</param>
        /// <returns>true表示为空，false表示不为空</returns>
        public static bool IsNull(this GameObject obj)
        {
            return obj == null;
        }
    }
}

